home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 7: Sunsite
/
Linux Cubed Series 7 - Sunsite Vol 1.iso
/
system
/
admin
/
linuxcon.000
/
linuxcon
/
linuxconf-1.6
/
netconf
/
net.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-06-11
|
2KB
|
106 lines
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "netconf.h"
#include "../xconf/xconf.h"
#include "netconf.m"
/*
Return != 0 if str is a valid ip number (lexically)
*/
int net_isipnum(const char *str)
{
int ret = 0;
int nbpt = 0; // Number of period met
while (1){
if (!isdigit(*str)){
break;
}else{
int num = atoi(str);
while (isdigit(*str)) str++;
if (num > 255){
break;
}else if (*str == '\0'){
ret = 1;
break;
}else if (*str != '.'){
break;
}else{
nbpt++;
str++;
}
}
}
if (ret && nbpt != 3) ret = 0;
return ret;
}
/*
Add a message in /usr/adm/netconf.log
*/
void net_prtlog (const char *ctl, ...)
{
char buf[10000];
va_list list;
va_start (list,ctl);
vsprintf (buf,ctl,list);
va_end (list);
if (simul_ison()){
simul_addmsg (buf);
}else{
FILE *fout = fopen (TMP_NETCONF_LOG,"a");
static char shown_err = 0;
/* #Specification: /usr/adm/netconf.log / can't open
Action taken by netconf are log in /usr/adm/netconf.log.
They can't be log with syslogd because syslogd is started
by netconf and may not be available.
If the file can't be open, one and only one error message will
be shown (per netconf session).
*/
if (fout != NULL){
fputs (buf,fout);
fclose (fout);
}else if (!shown_err){
shown_err = 1;
xconf_error (MSG_U(E_CANTOPEN,"Can't open file %s\n(%s)\n")
,TMP_NETCONF_LOG,strerror(errno));
}
}
}
/*
Send a title string with a date in /var/adm/netconf.log
*/
void net_introlog (const char *msg)
{
char cmd[100];
sprintf (cmd,"echo %s: `date`>>%s",msg,TMP_NETCONF_LOG);
system (cmd);
}
#ifdef TEST
static void test (const char *str)
{
printf ("testing %s -> %s\n",str
,net_isipnum(str) ? "OK" : "Not valid");
}
int main(int argc, char *argv[])
{
test ("hello");
test ("1.2.3");
test ("1.a.2.3");
test ("1a.2.3.4");
test ("11.22.33.444");
printf ("All other are valid\n");
test ("1.2.3.4");
test ("11.22.33.44");
return 0;
}
#endif